package br.com.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.com.bean.Aula;
import br.com.bean.Disciplina;
import br.com.bean.Pessoa;
import br.com.factory.ConnectionFactory;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class ProfessorServico {
	static ConnectionFactory banco;
	private static ProfessorServico instance;

	public static ProfessorServico getInstance() {
		if (instance == null) {
			banco = new ConnectionFactory();
			instance = new ProfessorServico(banco);
		}
		return instance;
	}
	
	public Connection connection;

	public ProfessorServico(ConnectionFactory banco) {
		this.connection = (Connection) banco.getConnection();
	}

	public ProfessorServico() {
		this.connection = (Connection) banco.getConnection();
	}
	
	public void cadastrarAula(Aula aula) {

		try {

			String sql = "INSERT INTO tb_aula (descricao,dataHora,nomeDisciplina, quantidade) "
					+ "VALUES (?,?,?,?)";

			PreparedStatement stmt = (PreparedStatement) connection
					.prepareStatement(sql);
			
			stmt.setString(1, aula.getDescricao());
			stmt.setDate(2, new java.sql.Date(System.currentTimeMillis()));
			stmt.setString(3, aula.getNomeDisciplina());
			stmt.setInt(4, aula.getQuantidade());
			
			stmt.execute();
			stmt.close();

		} catch (SQLException sqle) {
			throw new RuntimeException(sqle);
		}

	}
	
	public ArrayList<Disciplina> listarDisciplinas(Pessoa pessoa) {

		Disciplina disciplinaAux = null;
		ArrayList<Disciplina> disciplinas = new ArrayList<Disciplina>();

		try {

			String sql = "SELECT * FROM tb_disciplina WHERE matriculaProfessor = ?";

			PreparedStatement stmt = (PreparedStatement) connection
					.prepareStatement(sql);

			stmt.setString(1, pessoa.getMatricula());
			
			ResultSet rs = stmt.executeQuery();
			
			while (rs.next()) {
				disciplinaAux = new Disciplina();
				disciplinaAux.setNome(rs.getString("nomeDisciplina"));
				disciplinaAux.setPeriodicidade(rs.getString("periodicidade"));
				disciplinaAux.setPeriodo(rs.getString("periodo"));
				disciplinaAux.setCargaHoraria(rs.getString("cargaHoraria"));
				
				disciplinas.add(disciplinaAux);
			}

		} catch (SQLException sqle) {
			throw new RuntimeException(sqle);
		}

		return disciplinas;
	}
	
}
